-
Notifications
You must be signed in to change notification settings - Fork 184
Fix BufferSource algorithms for shared and resizable buffers
#1529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@annevk Could you assign reviewer(s) to this? Thanks! 🙏 |
annevk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did an editorial pass.
Co-authored-by: Anne van Kesteren <[email protected]>
Co-authored-by: Anne van Kesteren <[email protected]>
MattiasBuelens
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick self-review.
| 1. [=/Assert=]: |jsBufferSource| is a [=typed array type=] instance. | ||
| 1. Let |taRecord| be [$MakeTypedArrayWithBufferWitnessRecord$](|jsBufferSource|, | ||
| Seq-Cst). | ||
| 1. Return [$TypedArrayByteLength$](|taRecord|). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypedArrayByteLength asserts that IsTypedArrayOutOfBounds(taRecord) is false, which we didn't explicitly check. We should check for that, to align with %TypedArray%.prototype.byteLength.
| 1. Return [$TypedArrayByteLength$](|taRecord|). | |
| 1. If [$IsTypedArrayOutOfBounds$](|taRecord|) is true, return 0. | |
| 1. Return [$TypedArrayByteLength$](|taRecord|). |
| 1. Otherwise, if [$IsSharedArrayBuffer$](|jsBufferSource|) is true: | ||
| 1. [=/Assert=]: |jsBufferSource| is a {{SharedArrayBuffer}}. | ||
| 1. Return [$ArrayBufferByteLength$](|jsBufferSource|, Seq-Cst). | ||
| 1. Otherwise: | ||
| 1. [=/Assert=]: |jsBufferSource| is an {{ArrayBuffer}}. | ||
| 1. Return |jsBufferSource|.\[[ArrayBufferByteLength]]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ArrayBufferByteLength supports both ArrayBuffer and SharedArrayBuffer, so we can combine these steps.
| 1. Otherwise, if [$IsSharedArrayBuffer$](|jsBufferSource|) is true: | |
| 1. [=/Assert=]: |jsBufferSource| is a {{SharedArrayBuffer}}. | |
| 1. Return [$ArrayBufferByteLength$](|jsBufferSource|, Seq-Cst). | |
| 1. Otherwise: | |
| 1. [=/Assert=]: |jsBufferSource| is an {{ArrayBuffer}}. | |
| 1. Return |jsBufferSource|.\[[ArrayBufferByteLength]]. | |
| 1. Otherwise: | |
| 1. [=/Assert=]: |jsBufferSource| is an {{ArrayBuffer}} or a {{SharedArrayBuffer}}. | |
| 1. Return [$ArrayBufferByteLength$](|jsBufferSource|, Seq-Cst). |
Previously, many
BufferSourcealgorithms were using internal slots such as[[ByteOffset]]and[[ByteLength]]directly. However, with the addition ofSharedArrayBuffer(#353, #1311) and[AllowResizable](#982), this comes with extra caveats:SharedArrayBuffer, we need to useArrayBufferByteLengthto matchSharedArrayBuffer.prototype.byteLength.TypedArrayByteLength. Similarly, for a length-trackingDataView, we need to useGetViewByteLength.DataView, we first need to check if the view is still inbounds usingIsArrayBufferViewOutOfBounds.Transferring an
ArrayBuffermust also take into account whether it should remain resizable or not. Fortunately, we can use the newArrayBufferCopyAndDetachoperation for that.preserveResizabilityparameter to "transfer anArrayBuffer", so specifications can choose whether to preserve resizability or not. By default, this is not preserved. (Most specs aren't using[AllowResizable]yet anyway.)realmparameter of that algorithm, sinceArrayBufferCopyAndDetachdoesn't accept such a parameter. According to WebDex, no specs were actually using that parameter anyway.Open questions:
SharedArrayBuffer, what memory ordering should we use? ECMAScript seems to useSEQ-CSTpretty much everywhere (e.g.%TypedArray%.prototype.byteLength), but "get a copy of the bytes" usesUNORDEREDforGetValueFromBuffer.Fixes #1312.
Fixes #1385.
(See WHATWG Working Mode: Changes for more details.)
Preview | Diff